package treeHoleDAO;

import treeHoleSERVICE.PostMessageIMPH;
import treeHoleSERVICE.User;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;

/**
 * @program:IntelliJ IDEA
 * @Description:实现PersonalInformation类
 * @author:Mr.CheCheHuiBujianO
 * @data: 2022/6/19 15:18
 */
public class PersonalInformationIMPH implements PersonalInformation {
    class MyObjectOutputStream extends ObjectOutputStream{
        public MyObjectOutputStream(OutputStream out) throws IOException {
            super(out);
        }
        protected MyObjectOutputStream() throws IOException, SecurityException {
            super.reset();
        }

        @Override
        protected void writeStreamHeader() throws IOException {
            return;
        }
    }
    class MyObjectInputStream extends ObjectInputStream{


        public MyObjectInputStream(InputStream in) throws IOException {
            super(in);
        }
        @Override
        protected void readStreamHeader() throws IOException, StreamCorruptedException {
        }
    }

    /**
     * @param userAccount
     * @Description: 根据个人账号创建用户相应的文本文件（每个人只有一份）
     * @Param: [userName]
     * @return:[java.lang.String]
     * @Author: Mr.zhou
     * @Date: 2022/6/16 13:41
     */
    @Override
    public void PersonalInformationFile(int userAccount) {
        File file = new File("src/main/java/treeHoleDAO/"+userAccount+".txt");
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @param nowTime
     * @param userAccount
     * @param title
     * @param essayContent
     * @Description: 将用户的帖子写入属于其文档之中，记得参数之间需要使用我们的特殊符号进行间隔
     * @Param: [nowTime, title, essayContent]
     * @return:void
     * @Author: Mr.zhou
     * @Date: 2022/6/16 20:21
     */
    @Override
    public void WritePersonalInformationFile(int userAccount, Date nowTime, String title, String essayContent) {
        try {
            MyObjectOutputStream objectOutput = new MyObjectOutputStream(
                    new FileOutputStream("src/main/java/treeHoleDAO/"+userAccount+".txt",true));
            User user = new User();
            user.userEssayCollection = new ArrayList<>();
            User.UserHistory userHistory = user.new UserHistory();
            userHistory.title = title;
            userHistory.essayContent = essayContent;
            userHistory.releaseTime = nowTime;
            userHistory.giveLikeAccount = 0;
            //将我们的发帖直接写入
            objectOutput.writeObject(userHistory);
            objectOutput.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param userName
     * @param title
     * @Description: 点赞数量统计 跟在我们帖子文件中后面记录
     * @Param: []
     * @return:[]
     * @Author: Mr.jiang
     * @Date: 2022/6/16 13:37
     */
    @Override
    public void giveLikeAmount(String userName, String title,int userAccount) {
        User user = readUserHistoryEssayAndShowUI(userAccount);
        if (new PostMessageIMPH().giveLike(userName,title)){
            for (int i = 0; i < user.userEssayCollection.size(); i++) {
                if (title.equals(user.userEssayCollection.get(i).title)){
                    user.userEssayCollection.get(i).giveLikeAccount++;
                }
            }
        }

    }

    /**
     * @Description: 将用户的数据进行读出操作，将其展示在我们个人页面之中 将我们数据返回出来便于删除操作
     * @Param: []
     * @return: java.util.LinkedHashMap<java.util.Date, java.util.TreeMap < java.lang.String, java.lang.String>>
     * @Author: Mr.jiang
     * @Date: 2022/6/16 20:24
     */
    @Override
    public User readUserHistoryEssayAndShowUI(int userAccount) {
        User user = new User();
        try {
            MyObjectInputStream objectInput = new MyObjectInputStream(
                    new FileInputStream("src/main/java/treeHoleDAO/"+userAccount+".txt"));
            Object userHistory = null;
            user.userEssayCollection = new ArrayList<>();
            //User.UserHistory o = (User.UserHistory) objectInput.readObject();
            //System.out.println(o.title);
            while ( (userHistory = objectInput.readObject())!= null){
                userHistory = (User.UserHistory) userHistory;
                user.userEssayCollection.add((User.UserHistory)userHistory);
            }
//            objectInput.close();
        } catch (Exception e) {

        }
        return user;
    }

    /**
     * @param title
     * @Description: 根据我们题目内容进行一个删除，记得删除要将相应文本内容一起删除，记得再次调用展示函数和写入函数
     * @Param: [title]
     * @return:void
     * @Author: Mr.jiang
     * @Date: 2022/6/16 20:17
     */
    @Override
    public void deleteTreeHoleContent(int userAccount,String title) {
        User user = readUserHistoryEssayAndShowUI(userAccount);
        user.userEssayCollection.remove(title);
    }
}
